<template>
  <WLoading  v-if='!goodDetailInfo'/>
  <!-- 最外面的通栏盒子 -->
  <div class="app" v-if="goodDetailInfo">
    <!-- 中间的版心 -->
    <div class="wrap">
      <!-- 头部的导航 -->
      <ul class="nav">
        <li>首页 ></li>
        <li v-for="category in categoryList">
          <a @click="toGoodList(category)">{{ category.name }} ></a>
        </li>
        <li>{{ goodDetailInfo.name }}</li>
      </ul>
      <!-- 商品详情 -->
      <div class="detail">
        <div class="detailHd">
          <!-- 放大镜 -->
          <Loupe
            :itemDetail="itemDetail!"
            :defaultPicUrl="defaultPicUrl"
            :setDefalutImg="setDefalutImg"
          />
          <!-- 右侧商品参数 -->
          <div class="info">
            <div class="intro">
              <div class="intro-left">
                <h1>{{ goodDetailInfo.name }}</h1>
                <div>{{ goodDetailInfo.simpleDesc }}</div>
              </div>
              <div class="intro-right">
                <div>99.8%</div>
                <div>好评率 ></div>
              </div>
            </div>

            <div class="price">
              <div class="clearfix">
                <span class="label">价格</span>
                <span class="number">￥{{ goodDetailInfo.retailPrice }}</span>
                <span class="num">￥{{ goodDetailInfo.counterPrice }}</span>
              </div>
              <div class="feedbackBonus">
                <span class="feedbackBonus-left">购物返</span>
                <span class="feedbackBonus-right"
                  >最高返<span class="feedbackBonus-right-price"
                    >5积分
                  </span></span
                >
                <span class="iconfont icon-question-mark-circle"></span>
              </div>
              <div class="field">
                <span class="field-left">限制</span>
                <span>{{ goodDetailInfo!.itemLimit }}</span>
              </div>
              <div class="field">
                <span class="field-left">{{
                  goodDetailInfo.skuFreight.title
                }}</span>
                <a field-left-a>{{ goodDetailInfo.skuFreight.freightInfo }}</a>
              </div>
              <div class="field">
                <span class="field-left">配送</span>
                <div class="field-right-address">
                  至
                  <div>请选择地址<span>v</span></div>
                </div>
              </div>
              <hr />
              <div class="field food">
                <span class="field-left">服务</span>
                <span class="field-right"
                  >･ 严选自营 ･ 不支持无忧退换 ･ 不可用券 ･ 国内部分地区不可配送
                </span>
              </div>
            </div>
            <div class="specProp">
              <span class="type">规格</span>
              <ul class="tabs" @click="setDefalutImg">
                <li
                  v-for="sku in skuList"
                  :style="{
                    border:
                      sku.pic === defaultPicUrl ? ' 1px solid #ADA078' : '',
                  }"
                >
                  <img
                    :src="sku.pic"
                    :title="sku.itemSkuSpecValueList[0].skuSpecValue.value"
                    v-if="sku.pic"
                  />
                  <span v-if="!sku.pic">{{
                    sku.itemSkuSpecValueList[0].skuSpecValue.value
                  }}</span>
                </li>
              </ul>
            </div>
            <div class="clearfix">
              <span class="clearfix-num">数量</span>
              <div class="selnum">
                <div class="minus" @click="skuNum = Math.max(1, --skuNum)">
                  <span class="iconfont icon-minus"></span>
                </div>
                <div class="ipt"><input type="text" :value="skuNum" /></div>
                <div class="plus" @click="skuNum++">
                  <span class="iconfont icon-add"></span>
                </div>
                <span style="margin-left: 10px">{{
                  goodDetailInfo.pieceUnitDesc
                }}</span>
              </div>
            </div>
            <ul class="btns">
              <li>立即购买</li>
              <li>
                <a>
                  <span class="iconfont icon-gouwuchefill"></span> 加入购物车
                </a>
              </li>
              <li>
                <span class="iconfont icon-a-wujiaoxingxingxing"></span>
                收藏
              </li>
              <li>
                <span class="iconfont icon-xiazai-wenjianxiazai-07"></span>
                下载严选APP
              </li>
            </ul>
          </div>
        </div>
        <h1 class="promHd">你可能还喜欢</h1>
        <Carousel :likes="likes" />
        <!-- 详情底部左侧 -->
        <div class="detail-foot-left">
          <div class="innerWrap">
            <span>详情</span>
            <span>评价</span>
            <span>常见问题</span>
          </div>
          <div class="tabContent">
            <ul>
              <li class="item" v-for="(attr, index) in attrList">
                <span class="name">{{ attr.attrName }}</span>
                <span class="value">{{ attr.attrValue }}</span>
              </li>
            </ul>
            <div
              class="imgWrap"
              v-html="itemDetail && itemDetail.detailHtml"
            ></div>
          </div>
        </div>
        <!-- 详情底部右侧 -->
        <div class="detail-foot-right">
          <!-- 24小时热销榜 -->
          <div class="hotSell">
            <div class="hd">24小时热销榜</div>
            <ul>
              <li
                class="item"
                v-for="good in hotGoodList"
                v-hover:img.over="good"
                v-hover:img.out="good"
                v-to-detail="router"
              >
                <img :src="good.listPicUrl" alt="" :data-id="good.id" />
                <div class="content">
                  <a :data-id="good.id">{{ good.name }}</a>
                  <div class="content-price">
                    <span class="current">
                      <span class="iconfont icon-renminbi"> </span
                      >{{ good.retailPrice }}</span
                    >
                    <span class="original">
                      <span class="iconfont icon-renminbi"></span
                      >{{ good.counterPrice }}</span
                    >
                  </div>
                </div>
              </li>
            </ul>
          </div>
          <!-- 专题推荐 -->
          <div class="topic">
            <div class="hd">专题推荐</div>
            <ul class="bd">
              <li v-for="lively in livelys">
                <a :href="lively.linkUrl" target="_blank">
                  <img :src="lively.scenePicUrl" alt="" />
                  <div class="text">{{ lively.title }}</div>
                </a>
              </li>
            </ul>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
  export default {
    name: "WDetail",
  };
</script>

<script lang="ts" setup>
  import Carousel from "./Carousel/index.vue";
  import Loupe from "./Loupe/index.vue";
  import { ref, watch } from "vue";
  import { useRoute, useRouter } from "vue-router";
  import { useDetailStore } from "@/stores/modules/detail";
  import { storeToRefs } from "pinia";
  const route = useRoute();
  const router = useRouter();
  const detailStore = useDetailStore();
  const { getGoodDetailInfo, getLikes } = detailStore;
  const {
    goodDetailInfo,
    itemDetail,
    categoryList,
    defaultPicUrl,
    attrList,
    skuList,
    likes,
    hotGoodList,
    livelys,
  } = storeToRefs(detailStore);
  const skuNum = ref(1);
  // 设置默认图片
  const setDefalutImg = (e) => {
    let imgUrl = e.path.find((ele) => ele.nodeName === "IMG")?.src;
    imgUrl && (defaultPicUrl.value = imgUrl);
  };
  //路由跳转到商品详情页面
  const toGoodList = (category: any) => {
    if (category.level === "L1") {
      router.push({
        name: "WGoodsList",
        query: {
          categoryId: category.id,
        },
      });
    } else {
      router.push({
        name: "WGoodsList",
        query: {
          categoryId: category.superCategoryId,
          subCategoryId: category.id,
        },
      });
    }
  };
  //监听路由路径变化
  watch(
    () => route.query.id,
    (id) => {
      getGoodDetailInfo(id as unknown as number);
      getLikes(id as unknown as number);
    },
    {
      immediate: true,
    }
  );
</script>

<style lang="less" scoped>
  .app,
  .detail,
  .detailHd {
    width: 100%;
    overflow: hidden;
  }
  a {
    color: #333;
    &:hover {
      cursor: pointer;
      text-decoration: underline;
    }
  }
  .wrap {
    width: 1090px;
    margin: 0 auto;
  }
  .nav {
    width: 100%;
    padding: 20px 0;
    overflow: hidden;
  }
  .nav li {
    float: left;
    font-size: 12px;
    color: #333;
    margin-right: 6px;
  }

  .info {
    width: 620px;
    float: right;
  }
  .intro {
    overflow: hidden;
    margin-bottom: 20px;
  }
  .intro-left {
    float: left;
  }
  .intro-left h1 {
    font-size: 20px;
    font-weight: bold;
    color: #333;
    margin: 8px 0;
  }
  .intro-left div {
    font-size: 14px;
    color: #999;
  }
  .intro-right {
    float: right;
  }
  .intro-right div:first-child {
    font-size: 20px;
    color: #e36844;
    margin: 8px 0;
  }
  .intro-right div:last-child {
    font-size: 13px;
    color: #333;
  }
  .price {
    background-color: #f5f3ef;
    font-size: 13px;
    padding: 10px;
    color: #666;
  }
  .clearfix {
    height: 50px;
    line-height: 50px;
  }
  .label {
    margin-right: 20px;
  }
  .number {
    font-size: 28px;
    color: #d7282d;
    font-weight: bold;
    margin-right: 10px;
  }
  .num {
    color: #999;
    text-decoration: line-through;
  }
  .feedbackBonus,
  .field {
    height: 24px;
    line-height: 24px;
  }
  .feedbackBonus-left {
    margin-right: 12px;
  }
  .feedbackBonus-right {
    color: #333;
  }
  .feedbackBonus-right-price {
    color: #c43c3b;
  }
  .field-left {
    margin-right: 25px;
  }
  .field-right-address {
    display: inline-block;
    div {
      display: inline-block;
      box-sizing: border-box;
      width: 108px;
      height: 24px;
      font-size: 12px;
      margin: 0 10px 0 6px;
      padding: 0 14px;
      background-color: #fff;
      span {
        margin-left: 12px;
      }
    }
  }
  .food {
    height: 35px;
    line-height: 30px;
    .field-right:hover {
      cursor: pointer;
      text-decoration: underline;
    }
  }
  .specProp {
    font-size: 12px;
    margin: 20px 0;
    overflow: hidden;
  }
  .type,
  .tabs {
    float: left;
  }
  .type {
    width: 54px;
    height: 30px;
    line-height: 30px;
    color: #666;
  }
  .tabs li {
    float: left;
    margin: 5px 5px;
    border: 1px solid #ddd;
    img {
      width: 50px;
      height: 50px;
    }
    span {
      display: inline-block;
      padding: 10px 30px;
    }
    background-color: #f4f4f4;
    cursor: pointer;
  }
  .clearfix {
    height: 30px;
    line-height: 30px;
    span,
    div {
      float: left;
    }
    .clearfix-num {
      font-size: 12px;
      width: 50px;
    }
    .selnum div {
      border: 1px solid #ddd;
    }
  }
  .minus,
  .plus {
    width: 36px;
    height: 32px;
    box-sizing: border-box;
    font-size: 25px;
    padding-left: 10px;
    color: #999;
    cursor: pointer;
  }
  .ipt {
    width: 60px;
    height: 100%;
    input {
      width: 100%;
      height: 100%;
      font-size: 13px;
      outline: none;
      text-align: center;
      color: #333;
    }
  }
  .btns {
    height: 50px;
    margin: 30px 0 0 12px;
    li {
      float: left;
      height: 100%;
      line-height: 50px;
      margin-right: 10px;
      text-align: center;
      box-sizing: border-box;
      border-radius: 2px;
      .iconfont {
        font-size: 20px;
      }
    }
    li:nth-child(1) {
      width: 168px;
      font-size: 18px;
      color: #b4a08b;
      background-color: #f5f3ef;
      border: 1px solid #b4a08b;
    }
    li:nth-child(2) {
      width: 168px;
      font-size: 18px;
      a {
        display: block;
        width: 100%;
        height: 100%;
        color: #fff;
        background-color: #b4a078;
        &:hover {
          text-decoration: none;
        }
      }
    }
    li:nth-child(3) {
      width: 60px;
      font-size: 13px;
      color: #999;
      border: 1px solid #ccc;
    }
    li:nth-child(4) {
      width: 168px;
      color: #9fa19f;
      border: 1px solid #ccc;
    }
  }
  .promHd {
    height: 44px;
    line-height: 44px;
    font-size: 17px;
    padding-left: 20px;
    margin-top: 80px;
    background-color: #f5f5f5;
  }
  .detail-foot-left {
    float: left;
    width: 750px;
    .innerWrap {
      height: 43px;
      background-color: #f5f5f5;
      border: 1px solid #e8e8e8;
      margin-bottom: 18px;
      span {
        display: inline-block;
        font-size: 14px;
        width: 125px;
        height: 100%;
        text-align: center;
        line-height: 36px;
        &:nth-child(1) {
          color: #b4a078;
          background-color: #fff;
          border-top: 3px solid #b4a078;
          border-bottom: 1px solid #fff;
          margin-top: -1px;
          margin-bottom: -1px;
        }
        &:nth-child(2),
        &:nth-child(3) {
          color: #333;
          border: 1px solid #e8e8e8;
          border-style: none solid none solid;
        }
      }
    }

    .tabContent li {
      font-size: 14px;
      line-height: 24px;
      border-bottom: 1px solid #e8e8e8;
      overflow: hidden;
      float: left;
      min-width: 315px;
      padding: 8px 30px;
      .name {
        color: #333;
      }
      .value {
        color: #999;
      }
      span.name {
        display: inline-block;
        vertical-align: top;
        width: 105px;
      }
      span.value {
        display: inline-block;
      }
    }
    .imgWrap {
      font-size: 0;
      margin: 30px 0 80px 0;
      img {
        width: 750px;
      }
    }
  }
  .detail-foot-right {
    float: right;
    width: 298px;
    .hotSell,
    .topic {
      height: 100%;
      border: 1px solid #e8e8e8;
      margin-bottom: 30px;
    }
    .hd {
      width: 298px;
      height: 41px;
      text-align: center;
      line-height: 41px;
      font-size: 18px;
      font-weight: bold;
      color: #333;
      background-color: #f5f5f5;
      border-bottom: 1px solid #e8e8e8;
    }
    .item {
      margin: 24px 24px 0;
      img {
        width: 250px;
        height: 250px;
        cursor: pointer;
      }
      .content {
        padding: 0 5px 18px;
        font-size: 14px;
        text-align: center;
        a {
          display: inline-block;
          line-height: 20px;
          margin: 6px 37px 8px;
          &:hover {
            text-decoration: none;
            color: #b4a078;
          }
        }
        .icon-renminbi {
          font-size: 12px;
          margin-right: -2px;
        }
        .current {
          color: #d62525;
          margin-right: 5px;
        }
        .original {
          color: #666;
          text-decoration: line-through;
        }
      }
    }
    .bd {
      box-sizing: border-box;
      padding: 30px 19px 10px;
      width: 100%;
      li {
        position: relative;
        width: 100%;
        margin-bottom: 20px;
        img {
          width: 100%;
        }
      }
      .text {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        height: 30px;
        line-height: 30px;
        font-size: 14px;
        color: #fff;
        text-align: center;
        background-color: rgba(0, 0, 0, 0.6);
      }
    }
  }
</style>
